package my.xh.validate.handler.impl;

import my.xh.validate.ValidateField;
import my.xh.validate.handler.ValidateHandler;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Map;

/**
 * @ClassName IntValsValidateHandler
 * @Date 2019/1/17 15:29
 * @Author xuanhu
 * @Description 整数列举值的校验
 */
@Component
public class IntValsValidateHandler implements ValidateHandler {
    @Override
    public boolean validate(ValidateField validateField, StringBuilder paramName, Object paramValue, Map<String, Object> map, boolean flag) {
        if (validateField.intVals().length>0) { // 判断列举的整数值
            try {
                boolean iscontain=false;
                for (int  ii : validateField.intVals()) {
                    if(Integer.valueOf(ii).equals(Integer.valueOf(paramValue.toString()))) {
                        iscontain=true ;
                        break;
                    }
                }
                if (!iscontain) {
                    flag = false;
                    map.put(paramName.toString(),  getMsg(validateField,paramName.append(" 的值不在：").append(Arrays.toString(validateField.intVals())).append(" 之内").toString()));
                }
            } catch (Exception e) {
                flag = false;
                map.put(paramName.toString(),  getMsg(validateField,paramName.append(" 的值不在：").append(Arrays.toString(validateField.intVals())).append(" 之内").toString()));
            }
        }
        return flag;
    }
}
